<?php
class BackLinkEngineGoogle extends LogicBackLinkEngine
{
	/**
	 * @param string $url
	 * @return SeoSiteRank
	 */
	public function process($url)
	{
		$url = trim(preg_replace('/https?:\/\//', '', $url)); 
		$googleUrl = "http://www.google.com/search?hl=en&lr=&ie=UTF-8&q=link:$url&filter=0";
		Ezer_Log::debug("Load URL [$googleUrl]");
		$content = file_get_contents($googleUrl);
		
		preg_match_all('/<h3 class="r[^"]*"><a href="([^"]+)"[^>]*>([^<]+)<\/a><\/h3>/imsU', $content, $matches);
		
		if(count($matches) != 3)
			return null;
			
		$links = array();
		foreach($matches[1] as $index => $val)
		{
			$backLink = new SeoSiteBackLink();
			$backLink->setEngineType($this->getType());
			$backLink->setSrcUrl($val);
			$links[] = $backLink;
		}
		Ezer_Log::debug("Found [" . count($links) . "] links");
		return $links;
	}
}